package com.aier.admin.entity.mapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import net.eicp.ghostboy.futil.StringUtil;

import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.core.RowMapper;

import com.aier.admin.entity.AdminParenting;

public class AdminParentingRowMapper implements RowMapper<AdminParenting> {

    @SuppressWarnings("unused")
    private int type;

    public AdminParentingRowMapper() {
    }

    public AdminParentingRowMapper(int type) {
        this.type = type;
    }

    @Override
    public AdminParenting mapRow(ResultSet rs, int rowNum) throws SQLException {
        return parseAdminUserInfo(new AdminParenting(), rs);
    }

    public static AdminParenting parseAdminUserInfo(AdminParenting f, ResultSet rs)
        throws SQLException {
        return parseAdminUserInfo(f, rs, null);
    }

    public static AdminParenting parseAdminUserInfo(AdminParenting f, ResultSet rs, String su)
        throws SQLException {

        if (StringUtil.isStringNull(su)) {
            su = "";
        } else {
            su += ".";
        }
        try {
            f.setSpid(rs.getLong(su + "spid"));
        } catch (SQLException e) {
            f.setSpid(null);
        }
        try {
            f.setDays(rs.getLong(su + "days"));
        } catch (SQLException e) {
            f.setDays(null);
        }
        try {
            String str = rs.getString(su+"content");
            if (str != null) {
                str = str.replaceAll("<", "&lt;");
                str = str.replaceAll(">", "&gt;");
            }
            
            f.setContent(str);
        } catch (SQLException e) {
            f.setContent(null);
        }
        try {
            f.setImage(rs.getString(su + "image"));
        } catch (SQLException e) {
            f.setImage(null);
        }
        try {
            f.setAgeInfo(rs.getString(su + "ageInfo"));
        } catch (SQLException e) {
            f.setAgeInfo(null);
        }
        return f;
    }

    public static Map<String, Object> getPar(Map<String, Object> par, AdminParenting f) {
        par.put("spid", f.getSpid());
        par.put("days", f.getDays());
        par.put("content", f.getContent());
        par.put("image", f.getImage());
        par.put("ageInfo", f.getAgeInfo());
        return par;
    }

    public static Map<String, Object> getArgs(Map<String, Object> args,
            AdminParenting f) {
        args.put("spid", f.getSpid());
        return args;
    }

    /**
     * 
     * 
     * @param tfp
     * @return
     */
    public static PreparedStatementCreator getPreparedStatementCreator(
            final AdminParenting f) {
        return new PreparedStatementCreator() {
            @Override
            public PreparedStatement createPreparedStatement(Connection con)
                throws SQLException {
                PreparedStatement ps = con.prepareStatement(parseSQL(f),
                        new String[] { "spid" });
                parsePS(ps, f);
                return ps;
            }

        };
    }
    
    private static String parseSQL(AdminParenting f) throws SQLException {
        StringBuffer sql = new StringBuffer("insert into sys_parenting(");
        int count = 0;
        if (null != f.getDays()) {
            sql.append("days,");
            count++;
        }
        if (null != f.getContent()) {
            sql.append("content,");
            count++;
        }
        if (null != f.getImage()) {
            sql.append("image,");
            count++;
        }
        sql = new StringBuffer(sql.substring(0, sql.length() - 1));
        sql.append(") value(");
        for (int i = 0; i < (count - 1); i++) {
            sql.append("?,");
        }
        return sql.append("?)").toString();
    }


    private static void parsePS(PreparedStatement ps, AdminParenting f)
        throws SQLException {
        int count = 1;
        if (null != f.getSpid()) {
            ps.setLong(count++, f.getSpid());
        }

        if (null != f.getDays()) {
            ps.setLong(count++, f.getDays());
        }
        if (null != f.getContent()) {
            ps.setString(count++, f.getContent());
        }
        if (null != f.getImage()) {
            ps.setString(count++, f.getImage());
        }
        if (null != f.getAgeInfo()) {
            ps.setString(count++, f.getAgeInfo());
        }
    }

}
